<template>
  <div class="app-container" ref="appContainer">
    <div class="conversion-table">
      <PropTable
        :columns="column"
        :data="list"
        :total="total"
        @reset="reset"
        @onSubmit="onSubmit"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
        @exportExcelAction="exportExcelAction"
      >
        <template v-slot:projectout="scope">
          <div v-if="!useEmpty(scope.row.projectout)">
            {{ scope.row.projectout === "0" ? "否" : "是" }}
          </div>
        </template>

        <template v-slot:leixingtype="scope">
          <div v-if="!useEmpty(scope.row.leixingtype)">
            {{ scope.row.leixingtype === "0" ? "任务印章" : "成果印章" }}
          </div>
        </template>

        <template v-slot:operation="scope">
          <div style="display: flex; justify-content: flex-end">
            <el-button
              v-if="
                (scope.row.projectout === '0' || useEmpty(scope.row.projectout))
                && userStore.rolescode!=15
              "
              link
              type="primary"
              icon="Edit"
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleDetails(scope.row)"
            >
              详情
            </el-button>
            <el-button
              v-if="userStore.userId === 1 && userStore.rolescode!=15"
              link
              type="success"
              icon="Delete"
              @click="handelDelete(scope.row)"
              >删除
            </el-button>
          </div>
        </template>
      </PropTable>
    </div>
  </div>
</template>
<script setup >
import { ref, reactive, onMounted, nextTick } from "vue";
//@ts-ignore
import PropTable from "@/components/Table/PropTable/index.vue";
//@ts-ignore
import { exportExcel } from "@/utils/exprotExcel";
import { useEmpty } from "@/utils/index";
import { ElMessageBox, ElMessage } from "element-plus";

import { useRouter, useRoute } from "vue-router";

import { sealListApi, sealDeleteApi } from "@/api/seal/index";
import useUserStore from "@/store/modules/user";

const { proxy } = getCurrentInstance();

const loading = ref(true);
const detailsvisible = ref(false);

const route = useRoute();
const router = useRouter();

const businessoptions = ref([]);
const userStore = useUserStore();

const title = ref("");
const column = [
  {
    name: "projectName",
    label: "项目名称",
    inSearch: true,
    valueType: "input",
  },
  {
    name: "taskName",
    label: "任务名称",
    inSearch: true,
    valueType: "input",
  },

  {
    name: "projectnumber",
    label: "项目编号",
    // inSearch: true,
    valueType: "input",
  },
  {
    name: "projectout",
    label: "印章成果",
    slot: true,
    // inSearch: true,
    // valueType: "input",
  },
  {
    name: "leixingtype",
    label: "印章类型",
    slot: true,

    // inSearch: true,
    // valueType: "input",
  },

  {
    name: "applytime",
    label: "申请时间",
    // inSearch: true,
    // valueType: "input",
  },
  { name: "operation", slot: true, fixed: "right", width: 200, label: "操作" },
];

const list = ref([]);

let pageNum = ref(1);
let pageSize = ref(10);
let total = ref(0);
let paramsSubmit;

const seal = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  };

  if (paramsSubmit) {
    for (let key in paramsSubmit) {
      if (paramsSubmit[key]) {
        params[key] = paramsSubmit[key];
      }
    }
  }

  const res = await sealListApi(params);
  if (res.code === 200) {
    list.value = res.rows;
    total.value = Number(res.total);
  } else {
    ElMessage.error(res.msg);
    // 进行提示
    loading.value = false;
  }
};

const reset = (val) => {
  val = "";
  seal();
};
const onSubmit = (val) => {
  if (val) {
    // val.projectCreateTime = formatDate(val.projectCreateTime);
    paramsSubmit = val;
    seal();
  }
};

const handleSizeChange = (valueSise) => {
  pageSize.value = valueSise;
  seal();
};
const handleCurrentChange = (valuePage) => {
  pageNum.value = valuePage;
  seal();
};

// 导出
function exportExcelAction(val) {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  };
  if (val) {
    for (let key in val) {
      if (val[key]) {
        params[key] = val[key];
      }
    }
  }

  proxy.download(
    "/system/seal/export",
    params,
    `印章管理_${
      new Date().getFullYear() +
      "-" +
      new Date().getMonth() +
      1 +
      "-" +
      new Date().getDate()
    }.xlsx`
  );
}

const handleEdit = (row) => {
  router.push({
    path: "/seal/sealEdit/sealEdit",
    query: {
      name: "3",
      taskId: row.taskId,
      id: row.projectId,
      ids: row.id,
      sealType: row.leixingtype,
    },
  });
};

const handleDetails = (row) => {
  router.push({
    path: "/seal/sealEdit/sealEdit",
    query: {
      name: "3",
      taskId: row.taskId,
      id: row.projectId,
      ids: row.id,
      show: "1",
      sealType: row.leixingtype,
    },
  });
};

// 删除
const handelDelete = (row) => {
  ElMessageBox.confirm("确定要删除吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      if (row && row.id) {
        const result = await sealDeleteApi(row.id);
        if (result.code === 200) {
          // 删除成功的提示
          ElMessage.success(result.msg);
          seal();
        }
      }
    })
    .catch((err) => {
      // 用户点击取消执行的操作
      //   ElMessage.error(result.msg);
      console.log(err);
    });
};

onMounted(async () => {
  await seal();
  //   setTimeout(() => {
  //     loading.value = false;
  //   }, 500);
});
</script>


